package com.dmzc.stage2.chapter23;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class ProgrammerTravel extends Thread {
    private final Latch latch;
    private final String programmer;
    private final String transportatioin;

    public ProgrammerTravel(Latch latch, String programmer, String transportatioin) {
        this.latch = latch;
        this.programmer = programmer;
        this.transportatioin = transportatioin;
    }

    @Override
    public void run() {
        System.out.println(programmer+"start take the tras"+transportatioin);
        try {
            TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(programmer+"arrived by"+transportatioin);
        latch.countDown();
    }

    public static void main(String[] args) throws InterruptedException {
        Latch latch=new CountDownLatch(4 );
        new ProgrammerTravel(latch,"Alex","Bus").start();
        new ProgrammerTravel(latch,"Gavin","Bus").start();
        new ProgrammerTravel(latch,"Jack","Bus").start();
        new ProgrammerTravel(latch,"Dillon","Bus").start();
        latch.await();
        System.out.println("stage1 finish");
    }
}
